package org.lex.swing;

import javax.swing.Action;
import javax.swing.JPopupMenu;

public class PopupMenuHelper {
	/**
	 * @param popup
	 * @param actions
	 * @param separator
	 * @return
	 */
	public static JPopupMenu insertFront(JPopupMenu popup, Action[] actions, boolean separator) {
		if (null == popup) {
			popup = new JPopupMenu();
			return append(popup, actions, false);
		}
		for (int i = 0; i < actions.length; i++)
			popup.insert(actions[i], i);
		if (true == separator)
			popup.addSeparator();
		return popup;
	}

	public static JPopupMenu insertFront(JPopupMenu popup, Action... actions) {
		return insertFront(popup, actions, true);
	}

	public static JPopupMenu append(JPopupMenu popup, Action[] actions, boolean separator) {
		if (null == popup) {
			popup = new JPopupMenu();
			separator = false;
		}
		if (true == separator)
			popup.addSeparator();
		for (Action a : actions)
			popup.add(a);
		return popup;
	}

	public static JPopupMenu append(JPopupMenu popup, Action... actions) {
		return append(popup, actions, true);
	}

	private PopupMenuHelper() {
	}
}
